package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.DownloadsModRequest;

public class GenericDownloadModRequestDAO extends AutoKeys implements
		org.jetnuke.dao.DownloadModRequestDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericDownloadModRequestDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DownloadModRequestModel#retrieveByPk(int)
	 */
	public DownloadsModRequest retrieveByPk(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPk(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("DownloadModRequestModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		DownloadsModRequest s = new DownloadsModRequest();

		if (rs.next()) {
			fillModRequestFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPk(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	private void fillModRequestFromResultSet(DownloadsModRequest s, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillModRequestFromResultSet(DownloadsModRequest, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setBROKENDOWNLOAD(rs.getInt("BROKENDOWNLOAD"));
		s.setCID(rs.getInt("CID"));
		s.setDESCRIPTION(rs.getString("DESCRIPTION"));
		s.setEMAIL(rs.getString("EMAIL"));
		s.setFILESIZE(rs.getInt("FILESIZE"));
		s.setHOMEPAGE(rs.getString("HOMEPAGE"));
		s.setLID(rs.getInt("LID"));
		s.setMODIFYSUBMITTER(rs.getString("MODIFYSUBMITTER"));
		s.setNAME(rs.getString("NAME"));
		s.setREQUESTID(rs.getInt("REQUESTID"));
		s.setSID(rs.getInt("SID"));
		s.setTITLE(rs.getString("TITLE"));
		s.setURL(rs.getString("URL"));
		s.setVERSION(rs.getString("VERSION"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillModRequestFromResultSet(DownloadsModRequest, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DownloadModRequestModel#countModified()
	 */
	public int countModified() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("countModified() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("DownloadModRequestModel.countModified"));

		ResultSet rs = p.executeQuery();
		rs.next();
		int r = rs.getInt(1);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("countModified() - end"); //$NON-NLS-1$
		}
		return r;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DownloadModRequestModel#countBroken()
	 */
	public int countBroken() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("countBroken() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("DownloadModRequestModel.countBroken"));

		ResultSet rs = p.executeQuery();
		rs.next();
		int r = rs.getInt(1);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("countBroken() - end"); //$NON-NLS-1$
		}
		return r;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DownloadModRequestModel#update(org.jetnuke.entities.DownloadsModRequest)
	 */
	public int addNew(DownloadsModRequest modRequest) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(DownloadsModRequest) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("DownloadModRequestModel.addNew");
		this
				.setAutoGeneratedKeysQuery(SystemGlobals
						.getSql("DownloadModRequestModel.lastGeneratedDownloadModRequestID"));
		initNewDownload(modRequest, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(DownloadsModRequest) - end"); //$NON-NLS-1$
		}
		return id;

	}

	private void initNewDownload(DownloadsModRequest dc, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("initNewDownload(DownloadsModRequest, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setInt(1, dc.getBROKENDOWNLOAD());
		p.setInt(2, dc.getCID());
		p.setString(3, dc.getDESCRIPTION());
		p.setString(4, dc.getEMAIL());
		p.setInt(5, dc.getFILESIZE());
		p.setString(6, dc.getHOMEPAGE());
		p.setInt(7, dc.getLID());
		p.setString(8, dc.getMODIFYSUBMITTER());
		p.setString(9, dc.getNAME());
		p.setInt(10, dc.getSID());
		p.setString(11, dc.getTITLE());
		p.setString(12, dc.getURL());
		p.setString(13, dc.getVERSION());

		if (logger.isDebugEnabled()) {
			logger
					.debug("initNewDownload(DownloadsModRequest, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DownloadModRequestModel#selectBrokenByLID(int)
	 */
	public List selectBrokenByLID(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectBrokenByLID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("DownloadModRequestModel.selectBrokenByLID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectBrokenByLID(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	private List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			DownloadsModRequest u = new DownloadsModRequest();
			fillModRequestFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DownloadModRequestModel#deleteByLid(int)
	 */
	public void deleteByID(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteByID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("DownloadModRequestModel.deleteByID"));
		p.setInt(1, id);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("deleteByID(int) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DownloadModRequestModel#selectModified()
	 */
	public List selectModified() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectModified() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("DownloadModRequestModel.selectModified"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectModified() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.DownloadModRequestModel#selectBroken()
	 */
	public List selectBroken() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectBroken() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("DownloadModRequestModel.selectBroken"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectBroken() - end"); //$NON-NLS-1$
		}
		return list;
	}

	public void deleteByLID(int lid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteByLID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("DownloadModRequestModel.deleteByLID"));
		p.setInt(1, lid);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("deleteByLID(int) - end"); //$NON-NLS-1$
		}
	}
}
